import QtQuick 1.0
import com.meego 1.0
import "CommomUtil.js" as Util
import "const.js" as Const
import "database.js" as DatabaseUtil
import "Authorization.js" as AuthorizationUtil

/**
  * 账号管理
  * shenjia.caosj 2011-08-14
  */

Page {
    id: accounts_page

    tools : BackPageToolBar {

    }

    orientationLock : PageOrientation.LockPortrait

    TitleBar {
        id: accounts_title
        anchors.top: parent.top
        anchors.left: parent.left
        width: parent.width
        titleName: "账号管理"
    }





    Rectangle {
        id :account_list_rect
        anchors.top: accounts_title.bottom
        anchors.topMargin: 10
        anchors.bottom: accounts_page.bottom
        anchors.left: accounts_page.left
        anchors.right: accounts_page.right

        ListModel{
           id:account_list_model
        }



       //列表内容
       ListView {
            id: account_list
            width: parent.width
            height: parent.height
            model: account_list_model
            anchors.horizontalCenter: parent.horizontalCenter ;anchors.top: parent.top
            delegate: Item {
                id:account_list_item
                width: parent.width ; height: 80
                MouseArea{
                    anchors.fill:account_list_item
                    onPressAndHold:{
                        //delete ?
                        DatabaseUtil.deleteAccount(username) ;
                        reloadAccounts() ;
                        console.debug("mouse  onPressAndHold") ;
                    }
                    onClicked: {
                        console.debug("mouse  onClicked") ;
                        var account = DatabaseUtil.getAccount(username) ;
                        if(account != null){
                            var accountPassword = account.password ;
                            //do Login
                            AuthorizationUtil.login(username , accountPassword , function(isSuccess , msg){
//TODO
                                                        if(isSuccess){

                                                        }else{

                                                        }
                                                    }) ;
                        }
                    }

                }
                //background
                BorderImage {
                    id: account_list_item_back
                    source: "../images/favorite/back2.png"
                    width: parent.width; height: parent.height
                    anchors.horizontalCenter: parent.horizontalCenter

                    RadioButton{
                        id: account_list_item_isDefault
                        checked:isDefault === "T" ? true : false
                        anchors.left: parent.left
                        anchors.leftMargin: 20
                        anchors.verticalCenter: parent.verticalCenter
                    }

                    //title name
                    NormalText{
                        id :account_list_item_name
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.left: account_list_item_isDefault.right
                        anchors.leftMargin: 15

                        text: username
                        color : Const.Color.darkBlue
                        font.pointSize: Const.FontSize.normal

                    }

                }

            }
            Component.onCompleted: {
                reloadAccounts() ;
            }

        }

    }


    /**
      * 刷新（重新载入）收藏
      */
    function reloadAccounts(){

        var accounts = DatabaseUtil.getAllAccounts() ;
        account_list_model.clear() ;
        for(var i=0 ;i<accounts.length;i++){
            var item_tmp = accounts[i] ;
            //遍历加到列表中
            account_list_model.append({
                 sequence : item_tmp.sequence ,
                 username :item_tmp.username ,
                 password : item_tmp.password ,
                 isDefault : item_tmp.isDefault
             }) ;
        }

    }
}
